package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService feeValueService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;

    //如果使用自定义分页 必须采用通用mapper
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    /**
     *  1.如果有id则是更新操作  则应该先删除中间表数据  之后新增
     *  2.如果没有id则应该新增数据.
     */
    @Override
    @Transactional
    public void insertItem(ApartmentSubmitVo apartmentSubmitVo) {
        //1.获取主表id值
        Long id = apartmentSubmitVo.getId();

        if(id !=null){
            //先删除中间表数据
            //1.删除图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<GraphInfo>();
            queryWrapper.eq(GraphInfo::getItemId,id)
                        .eq(GraphInfo::getItemType,ItemType.APARTMENT);
            graphInfoService.remove(queryWrapper);

            //2.删除标签中间表数据
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //3.删除配套信息
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            //4.删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            feeValueService.remove(feeValueLambdaQueryWrapper);
        }

        //根据id查询 省市区县信息
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());

        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        //3.执行新增操作 先新增主表  再新增关联表 通过主表的id进行关联.
        //主键自增之后 回显主键 MP自动实现主键的回显
        this.saveOrUpdate(apartmentSubmitVo);
        id = apartmentSubmitVo.getId();

        //4.图片新增
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

        //5.新增配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
        facilityInfoIds.forEach(facilityId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .facilityId(facilityId).build();
            apartmentFacilityList.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilityList);

        //6.新增标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> labelList = new ArrayList<>(labelIds.size());
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .labelId(labelId)
                    .build();
            labelList.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(labelList);

        //7.新增杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .feeValueId(feeValueId)
                    .build();
            apartmentFeeValueList.add(apartmentFeeValue);
        });
        feeValueService.saveBatch(apartmentFeeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(long current, long size, ApartmentQueryVo queryVo) {

        IPage<ApartmentItemVo> page = new Page<>(current,size);
        page = apartmentInfoMapper.pageItem(page,queryVo);
        return page;
    }

    @Override
    public ApartmentDetailVo findDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //1.查询主表信息
        ApartmentInfo apartmentInfo = this.getById(id);
        //2.将相同的属性赋值
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        //3.查询图片列表信息  公寓  房间
        List<GraphVo> graphVoList = graphInfoMapper.findItemById(id,ItemType.APARTMENT);

        //4.查询标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.findItemByApartmentId(id);

        //5.查询配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findItemByApartmentId(id,ItemType.APARTMENT);

        //6.查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.findItemByApartmentId(id);

        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    @Override
    public void removeItemById(Long id) {

        //1.查询公寓中是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if(count > 0 ){
            //有房间不能被删除  自定义异常 之后全局处理
            throw new LeaseException(ResultCodeEnum.ADMIN_HAS_ROOM);
        }

        //1.删除图片信息
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<GraphInfo>();
        queryWrapper.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoService.remove(queryWrapper);

        //2.删除标签中间表数据
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //3.删除配套信息
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);

        //4.删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        feeValueService.remove(feeValueLambdaQueryWrapper);

        //5.删除公寓信息
        this.removeById(id);
    }
}




